<?php

namespace YmPhp\Admin\Http\Controllers\Business;

use YmPhp\Admin\Admin;
use YmPhp\Admin\Http\Repositories\Business\BusinessPackage;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;
use function Symfony\Component\Translation\t;

class BusinessPackageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::style('
        .bs-popover-right {
            box-shadow: 0px 0px 20px 0px rgb(0 25 77 / 30%);
        }
        .popover-body {
            color: #545454;
        }
        .ie-container {
            display: flex;
            flex-wrap: wrap;
        }
        .ie-wrap [data-editinline="popover"]{
            max-height: 80px;
            overflow: hidden;
            text-overflow: ellipsis;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
');
        return Grid::make(new BusinessPackage(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name')->display(function($value) {
                $html = '<div style="display: flex;">';
                    $html .= '<div>';
                        if ($this->icon == ''){
                            $html .= '<img data-action="preview-img" src="/static/images/default.png" style="height: 80px;width:80px;border-radius: 5px;cursor:pointer" class="img img-thumbnail"/>';
                        }else{
                            $html .= '<img data-action="preview-img" src="'.$this->icon.'" style="height: 70px;width:70px;border-radius: 5px;cursor:pointer" class="img img-thumbnail"/>';
                        }
                    $html .= '</div>';
                    $html .= '<div style="margin-left: 10px;">';
                        $html .= '<span style="font-weight: bold;width: 140px;display: block;">'.$value.'</span><br>';
//                        $html .= '<span style="margin-top: 5px;width: 100%;">'.$this->info_price.'</span>';

                    $html .= '</div>';
                $html .= '</div>';
                return $html;

            })->width(250);
            $grid->column('access')->checkbox((new BusinessPackage())->privilege(), true)->width(260);
            $grid->column('days')->display(function($value) {
                $html = '有效期：'.$value .'天' ?? '永久';
                $html .= '<br>商家账期：'.$this->zhangqi;
                $html .= '<br>手续费比例：'.$this->insxf .'%' ?? '免手续费';
                $html .= '<br>发布信息折扣：'.$this->discount .'折' ?? '无折扣';
                return $html;
            });
            $grid->column('aprice')->display(function($value) {
                $html = '入住价格：'.$this->price .'元' ?? '免费';
                $html .= '<br>APP入驻价格：'.$value .'元' ?? '免费';
                $html .= '<br>每天免费发布数：'.$this->freeday .'条' ?? '未设置';
                if ($this->hy_price == ''){
                    $html .= '<br>行业价格：未设置';
                }else{
                    $html .= '<br>行业价格：'.$this->hy_price.'元';
                }

                if ($this->dig_price == ''){
                    $html .= '<br>置顶价格：未设置';
                }else{
                    $html .= '<br>置顶价格：'.$this->dig_price.'元';
                }
                return $html;

            });
            $grid->column('hide')->switch();
            $grid->column('hide2')->switch();
            $grid->column('maxsize');
            $grid->column('displayorder');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->showQuickEditButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BusinessPackage(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('access');
            $show->field('price');
            $show->field('days');
            $show->field('dig_price');
            $show->field('icon');
            $show->field('zhangqi');
            $show->field('insxf');
            $show->field('discount');
            $show->field('displayorder');
            $show->field('aprice');
            $show->field('freeday');
            $show->field('hide');
            $show->field('hide2');
            $show->field('maxsize');
            $show->field('hy_price');
            $show->field('desc');
            $show->field('info_price');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BusinessPackage(), function (Form $form) {
            $form->width(9, 2);
                $form->text('name');
                $form->text('zhangqi');
                $form->text('insxf');
                $form->text('discount');
                $form->text('days');
                $form->switch('hide');
                $form->switch('hide2');

                $form->currency('price')->symbol('￥');
                $form->text('displayorder');
                $form->text('aprice');
                $form->text('freeday');
                $form->text('maxsize');
                $form->textarea('desc');
                $form->textarea('info_price');
                $form->textarea('dig_price')->placeholder('每行一个,格式:
天数=金额');
                $form->textarea('hy_price')->placeholder('每行一个,格式:
行业id|价格|APP价格');
                $form->image('icon')->move('upload/business/package/')->autoUpload()->uniqueName()->saveFullUrl();
                $form->checkbox('access')->options((new BusinessPackage())->privilege());
//            });
        });
    }
}
